package com.onetoall.yjt;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by shaomengjie on 2016/12/28.
 */

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.MyViewHolder> {
    private Context mContext;
    private List<String> mData;
    private boolean flage = false;

    public MyRecycleViewAdapter(Context context, List<String> mData) {
        this.mContext = context;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = null;
        View view;
        if (viewType == 0){
            view = View.inflate(mContext, R.layout.recycleview_item,null);
            holder = new MyViewHolder(view);
        }else if (viewType == 1){
            view = View.inflate(mContext,R.layout.recycleview_item,null);
            holder = new MyViewHolder(view);
        }else if (viewType == 2){
            view = View.inflate(mContext,R.layout.recycleview_item,null);
            holder = new MyViewHolder(view);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (getItemViewType(position) == 0){
            initView_zero(holder,position);
        }else if (getItemViewType(position) == 1){
            initView_one(holder,position);
        }else if (getItemViewType(position) == 2){
            initView_two(holder,position);
        }
    }

    private void initView_one(MyViewHolder holder, int position) {
        holder.textView.setText(mData.get(position));
    }

    private void initView_two(MyViewHolder holder, int position) {
        holder.textView.setText(mData.get(position));
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!flage) {
                    view.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                }else {
                    view.setBackgroundColor(mContext.getResources().getColor(R.color.myWhite));
                }
               flage = !flage;
            }
        });
    }

    private void initView_zero(MyViewHolder holder, int position) {
        holder.textView.setText(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView ;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.item_textView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 15 == 0) {
            return 0;
        }else if (position<15){
            return 1;
        }else {
            return 2;
        }
    }

}
